package com.sdry.web.controller.gemini;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.base.BaseController;
import com.sdry.entity.*;
import com.sdry.service.gemini.UpmsCompanyService;
import com.sdry.utils.ResponseUtil;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.sdry.entity.UpmsCompanyExample.Criteria;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Gemini on 2018/7/31
 *
 * 公司信息控制类
 */

@Controller
@RequestMapping("/base/company")
public class BaseCompanyInfoController extends BaseController {

    @Resource
    private UpmsCompanyService companyService;
    /**
     * 去公司信息页面
     * @return
     */
    @RequestMapping(value = "/baseInfoCompany", method = RequestMethod.GET)
    public String baseInfoCompany() {

        return "/gemini/info_company";
    }

    /**
     * 添加公司信息
     * @param response 响应
     * @param company 公司信息实体
     * @throws Exception 异常
     */
    @RequestMapping("/insertBaseCompanyInfo")
    public void insertCompany(HttpServletResponse response, UpmsCompany company) throws Exception {

        //添加
        int affact = companyService.insert(company);
        ResponseUtil.write(response, affact);
    }

    /**
     * 带条件的分页查询公司信息
     * @param request 请求
     * @param response 响应
     * @param limit 每页记录数
     * @param offset 页面
     * @param company 公司信息实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectBaseCompanyInfo")
    public void selectCompanyList(HttpServletRequest request, HttpServletResponse response,
                               @RequestParam(value = "limit",required = false)int limit,
                               @RequestParam(value = "offset",required = false)int offset,
                                  UpmsCompany company) throws Exception {

        PageHelper.startPage(offset, limit);

        UpmsCompanyExample example = new UpmsCompanyExample();
        Criteria criteria = example.createCriteria();
        if (company.getCompanyName() != null && !("").equals(company.getCompanyName())){
            criteria.andCompanyNameLike("%"+company.getCompanyName()+"%");
        }

        if (company.getCompanyRepresent() != null && !("").equals(company.getCompanyRepresent())){
            criteria.andCompanyRepresentLike("%"+company.getCompanyRepresent()+"%");
        }


        List<UpmsCompany> companies = companyService.selectByExample(example);

        PageInfo pageInfo = new PageInfo(companies);

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", pageInfo.getTotal());
        jsonObject.put("rows", pageInfo.getList());
        ResponseUtil.write(response, jsonObject);
    }


    /**
     * 根据id查询公司信息
     * @param response 响应
     * @param id 公司id
     * @throws Exception 异常
     */
    @RequestMapping("/selectBaseCompanyInfoById")
    public void selectCompanyById(HttpServletResponse response, Long id) throws Exception {

        UpmsCompany company = companyService.selectByPrimaryKey(id);
        //将对象转化为json
        JSONObject json = JSONObject.fromObject(company);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改公司信息
     * @param response 响应
     * @param company 公司信息
     * @throws Exception 异常
     */
    @RequestMapping("/editBaseCompanyInfo")
    public void editCompany( HttpServletResponse response,
                             UpmsCompany company) throws Exception {
        //修改
        int affct = companyService.updateByPrimaryKey(company);
        ResponseUtil.write(response, affct);
    }

    /**
     * 批量删除公司信息
     * @param response 响应
     * @param ids 公司id
     * @throws Exception 异常
     */
    @RequestMapping("/deleteBaseCompanyInfo")
    public void deleteCompany( HttpServletResponse response, String ids) throws Exception {
        int affact = companyService.deleteByPrimaryKeys(ids);
        ResponseUtil.write(response, affact);
    }
}
